package servlet;

import dao.AddFarmDatebase;
import dao.AddFolkDateBase;
import model.Farm;
import model.Folk;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "AddFarmServlet")
public class AddFarmServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Farm farm=new Farm();
        AddFarmDatebase addFarmDatebase=new AddFarmDatebase();
        response.setContentType("textml");
        DiskFileItemFactory factory = new DiskFileItemFactory(55555535, new File("d://javaweb"));
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        } // 解析request请求
        Iterator iter = items.iterator();// 遍历表单中提交过来的内容
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                String value = item.getString("UTF-8");
                float value1 = item.hashCode();
                if (item.getFieldName().equals("farm_name")) {
                    System.out.println(value);
                    farm.setFarmName(value);
                } else if (item.getFieldName().equals("farm_price")) {
                    System.out.println(value1);
                    farm.setPrice(value1);
                } else if (item.getFieldName().equals("farm_type")) {
                    System.out.println(value);
                    farm.setBreed(value);
                } else if (item.getFieldName().equals("farm_eggs")) {
                    System.out.println(value);
                    farm.setEggs(Integer.parseInt(value));
                } else if (item.getFieldName().equals("farm_bed")) {
                    System.out.println(value);
                    farm.setAlreadBreed(value);
                } else if (item.getFieldName().equals("farm_area")) {
                    System.out.println(value);
                    farm.setArea(value);
                }
            } else {

                ServletContext sctx = getServletContext();
                String path = sctx.getRealPath("");
                String fileName = item.getName();
                String lind = fileName.substring(fileName.lastIndexOf("."));
                String picname = String.valueOf("upload" + "\\"+System.currentTimeMillis())
                        + lind;
                File file = new File(path + "\\"  + picname);
                System.out.println(picname);

                try {
                    item.write(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                farm.setFarmImg(picname);
            }
        }

        farm.setUsername((String) request.getSession().getAttribute("username"));
        System.out.println((String) request.getSession().getAttribute("username"));
        try {
            addFarmDatebase.addFarm(farm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/addFarm.jsp").forward(request, response);
    }
}
